package c_processPkg

import (
	"Infinichat_mixed/entity"
	"Infinichat_mixed/transfer"
	"encoding/json"
	"fmt"
	"net"
	"strings"
)

type SmsProcess struct {
}

func (this *SmsProcess) SendBroadcastEMs(content string) (err error) {
	var mes entity.Message
	mes.Type = entity.SmsMesType

	var smsMes entity.SmsMes
	content = strings.ReplaceAll(content, "\r\n", "")
	smsMes.Content = content
	smsMes.User = CurUser.User
	data, err := json.Marshal(smsMes)
	if err != nil {
		fmt.Println("SendBroadcastEMs json.Marshal fail:", err.Error())
		return
	}

	mes.Data = string(data)
	data, err = json.Marshal(mes)
	if err != nil {
		fmt.Println("SendBroadcastEMs json.Marshal fail:", err.Error())
		return
	}

	tf := &transfer.Transfer{
		Conn: CurUser.Conn,
	}

	err = tf.WriterPkg(data)
	if err != nil {
		fmt.Println("SendBroadcastEMs err:", err.Error())
		return
	}
	return
}

func (this *SmsProcess) Send2one(tag_id uint, content string) (err error) {
	return S2O(CurUser.Conn, tag_id, content)
}

func S2O(Conn net.Conn, tag_id uint, content string) (err error) {
	var mes entity.Message
	mes.Type = entity.SmsMes2OneType

	//var smsMes2one entity.SmsMes2one
	//smsMes2one.TagetId = tag_id
	//smsMes2one.Content = content
	//smsMes2one.User = CurUser.User

	var smsMes2one entity.P2PMsg
	smsMes2one.Content = content
	smsMes2one.ReceiveId = tag_id
	smsMes2one.SendId = CurUser.ID

	data, err := json.Marshal(smsMes2one)
	if err != nil {
		fmt.Println("Send2one json.Marshal fail:", err.Error())
		return
	}

	mes.Data = string(data)
	data, err = json.Marshal(mes)
	if err != nil {
		fmt.Println("Send2one json.Marshal fail:", err.Error())
		return
	}

	tf := &transfer.Transfer{
		Conn: Conn,
	}

	err = tf.WriterPkg(data)
	if err != nil {
		fmt.Println("Send2one err:", err.Error())
		return
	}
	return
}
